<?php
namespace app\wxapp\controller;

use think\Db;

class Battle 
{
    // public function index(){
        
    // }
    //添加对局的信息
    public function add(){
        $battle_info['event_id']     = input('event_id');
        $battle_info['event_stage']  = input('event_stage');
        $battle_info['sum_frame']    = input('sum_frame');
        $battle_info['player1_name'] = input('player1_name');
        $battle_info['player2_name'] = input('player2_name');
        $battle_info['update_time']  = time();
        $battle_info['battle_type']  = input('battle_type') == 2 ? 2:1;
        $battle_id                   = input('battle_id');

        if($result = Db::name('wxapp_battle')->insert($battle_info) && $id = Db::name('wxapp_battle')->getLastInsID()){
            $data['state'] = 1;
            $data['id']    = $update_info['next_id'] = $id;
            if($battle_id) db('Wxapp_battle')->where('id',$battle_id)->update($update_info);
            $data['info']  = '添加成功！';
        }else{
            $data['state'] = 0;
            $data['info']  = '添加失败！';
        }
        return json($data);
    }

    // //编辑对局的信息
    public function edit(){
        $id                = input('id');
        $battle_info['event_stage']  = input('event_stage');
        $battle_info['sum_frame']    = input('sum_frame');
        $battle_info['player1_name'] = input('player1_name');
        $battle_info['player2_name'] = input('player2_name');
        $battle_info['update_time']  = time();
        $result = db('Wxapp_battle')->where('id',$id)->update($battle_info);
        if($result || $result === 0){
            $data['state'] = 1;
            $data['info']  = '编辑成功！';
        }else{
            $data['state'] = 0;
            $data['info']  = '编辑失败！';
        }
        return json($data);
    }

    //编辑对局的比分信息
    public function edit_score(){
        $battle_id                    = input('battle_id');
        $battle_info['player1_score'] = input('player1_score');
        $battle_info['player2_score'] = input('player2_score');
        $battle_info['update_time']  = time();
        $result = db('wxapp_battle')->where('id',$battle_id)->update($battle_info);
        if($result || $result === 0){
            $data['state'] = 1;
            $data['info']  = '编辑成功！';
        }else{
            $data['state'] = 0;
            $data['info']  = '编辑失败！';
        }
        return json($data);
    }

    //编辑斯诺克的比分信息
    public function editSnookerScore(){
        $battle_id                    = input('battle_id');
        $battle_info['player1_score'] = input('player1_score');
        $battle_info['player2_score'] = input('player2_score');
        $battle_info['update_time']   = time();
        $battle_info['snooker_score'] = input('snooker_score');
        $result = db('wxapp_battle')->where('id',$battle_id)->update($battle_info);
        if($result || $result === 0){
            $data['state'] = 1;
            $data['info']  = '编辑成功！';
        }else{
            $data['state'] = 0;
            $data['info']  = '编辑失败！';
        }
        return json($data);
    }
    
    // //保存登录的code
    public function addLoginCode(){
        $id             = input('id');
        $event_id       = input('event_id');
        $login_code     = explode('|',input('login_code'));

        $customer_info     = $this->getCustomerInfo($event_id);//获取该客户的信息

        $update['status']  = $customer_info['status'];//该客户的状态

        if(count($login_code)>1){
            $update['login_code'] = $login_code[0];
        }else{
            $update['status'] = -1;
        }
  
        if($result = db('wxapp_battle')->where('id',$id)->update($update)){
            $data['state'] = 1;
            $data['info']  = '保存成功！';
        }else{
            $data['state'] = 0;
            $data['info']  = '保存失败！';
        }
        
        //如果用户状态正常，则计费
        if($update['status'] == 1){
            $consumption_log_info = $this->scoreboardLoginLog(input('event_id'),input('session_id'),$login_code[1]);//添加计分板登陆log
            //获取客户类型是A还是B还是C
            $customer_type        = db('customer_type')->where('id',$customer_info['customer_type_id'])->field('type,amount')->find();
            $consumption_log_info['price']  = $customer_type['type'] == 'A' ? $customer_type['amount'] : 0;
            
            $this->addConsumptionLog($consumption_log_info);//添加计费日志
            $this->addChargeLog($customer_info,$consumption_log_info['price']);//添加消费日志
        }

        return json($result);
    }

    //获取用户信息
    private function getCustomerInfo($event_id){
        $customer_id     =  db('event')->where('id',$event_id)->value('customer_id');
        $customer_info   =  db('customer')->where('id',$customer_id)->field('id,customer_name,mobile,status,customer_type_id,money')->find();
        return $customer_info;
    }

    //添加计分板登录log
    private function  scoreboardLoginLog($event_id,$session_id,$scoreboard_ip){
         //显示端登录日志
        $Wxapp_user    = db('wxapp_user');
        $Event         = db('event');

        $wx_user_info  = $Wxapp_user->where('session_id',$session_id)->field('openid,nickName,avatarUrl')->find();
        $event_info    = $Event->where('id',$event_id)->field('event_name,customer_id,auth_code')->find();

        $scoreboard_log_info['use_time']        = time();
        $scoreboard_log_info['event_id']        = $event_id;
        $scoreboard_log_info['auth_code']       = $event_info['auth_code'];
        $scoreboard_log_info['event_name']      = $event_info['event_name'];
        $scoreboard_log_info['customer_id']     = $event_info['customer_id'];
        // $scoreboard_log_info['customer_name']   = $event_info['customer_name'];
        // $scoreboard_log_info['customer_mobile'] = $event_info['customer_mobile'];

        $scoreboard_log_info['scoreboard_ip']     = $scoreboard_ip?$scoreboard_ip:'';

        $scoreboard_log_info['wx_nick_name']  = $wx_user_info['nickName'] ? $wx_user_info['nickName'] : '';
        $scoreboard_log_info['wx_avatar_url'] = $wx_user_info['avatarUrl'] ? $wx_user_info['avatarUrl']:'';
        $scoreboard_log_info['wx_openid']     = $wx_user_info['openid'] ? $wx_user_info['openid']:'';       
        Db::name('scoreboard_log')->insert($scoreboard_log_info);
        return $scoreboard_log_info;
    }

    //添加计费日志
    private function addConsumptionLog($consumption_log_info){
        unset($consumption_log_info['wx_nick_name']);
        unset($consumption_log_info['wx_openid']);
        unset($consumption_log_info['wx_avatar_url']);
        unset($consumption_log_info['openid']);
        return Db::name('consumption')->insert($consumption_log_info);
    }
    
    //添加消费记录
    private function addChargeLog($customer_info,$price){
        $update['money']  = $customer_info['money'] - $price;
        $update['status'] = $update['money'] < 0 ? 2 : $customer_info['status'];
        if(db('customer')->where('id',$customer_info['id'])->update($update)){
            //扣费成功，添加消费日志
            $insert_info['customer_id']     = $customer_info['id'];
            $insert_info['customer_name']   = $customer_info['customer_name'];
            $insert_info['customer_mobile'] = $customer_info['mobile'];
            $insert_info['charge_money']    = $price;
            $insert_info['remarks']         = '记分板登陆';
            $insert_info['charge_time']     = time();
            return Db::name('charge_log')->insert($insert_info);
        }

    }
}
